﻿using System;
using System.Collections.Generic;
using LogSystem;
using UnityEngine;
using Yoozoo.Framework.Core;
using Yoozoo.Gameplay.Liberty.AI;
using Yoozoo.Gameplay.Liberty.Pool;
using Yoozoo.Gta.Common;
using Yoozoo.HRP.Runtime;
using Yoozoo.Managers.ResourceManagerV2.Runtime;

namespace Yoozoo.Gameplay.Liberty.StreetEvent
{
    public class StreetEventHittableTargetManager : SingletonMono<StreetEventHittableTargetManager>
    {
        // 轨道列表
        [SerializeField]
        private List<StreetEventHittableTargetLine> _lines;
        
        // 波次Id列表
        [SerializeField]
        private List<int> _totalGroupIdSettings = new List<int>{1, 2, 3, 4};
        // 总波次索引
        [SerializeField]
        private int _totalGroupCount = 0;
        
        // 当前波次
        [SerializeField]
        private int _groupId = 1;
        public int GroupId => _groupId;
        
        // 当前是否可刷新
        [SerializeField]
        private bool _canRefresh = true;
        public bool CanRefresh
        {
            set => _canRefresh = value;
            get => _canRefresh;
        }

        // 轨道map
        private Dictionary<int, StreetEventHittableTargetLine> _linesMap;

        // 记录通知
        private HashSet<int> _noticedGroupIds;

        private void Awake()
        {
            p_instance = this;

            _linesMap = new Dictionary<int, StreetEventHittableTargetLine>();
            foreach (var line in _lines)
            {
                _linesMap[line._lineId] = line;
            }
            _noticedGroupIds = new HashSet<int>();
        }

        private void OnDestroy()
        {
            p_instance = null;
        }

        /// <summary>
        /// 创建靶子
        /// </summary>
        /// <param name="lineId"></param>
        /// <param name="type"></param>
        /// <param name="resPath"></param>
        /// <param name="lifeTime"></param>
        /// <param name="speed"></param>
        /// <param name="score"></param>
        /// <param name="head_score"></param>
        public void CreateHittableTarget(int lineId, int spawnPoint, int itype, string resPath, float lifeTime, float speed, int score, int head_score, int elite_target)
        {
            HittableTarget.EHittableTargetType type = (HittableTarget.EHittableTargetType)itype;
            LibertyPoolMgr.GetInstance().Pop(resPath, go =>
            {
                HittableTarget hittableTarget = go.GetComponent<HittableTarget>();
                hittableTarget.Show(lineId, spawnPoint, type, resPath, lifeTime, speed, score, head_score, elite_target);
                _linesMap[lineId].AddHittableTarget(hittableTarget);
            });
        }
        
        /// <summary>
        /// 从轨道中移除靶子
        /// </summary>
        /// <param name="hittableTarget"></param>
        public void RemoveHittableTargetFromLine(HittableTarget hittableTarget)
        {
            // 从轨道中移除
            {
                var line = _linesMap[hittableTarget.LineId];
                line.RemoveHittableTarget(hittableTarget);
            }
            
            // 尝试刷新轨道 (所有轨道的靶子全部打完，才会刷新下一波)
            {
                bool isAllEmpty = true;
                foreach (var line in _lines)
                {
                    if (!line.IsLineEmpty())
                    {
                        isAllEmpty = false;
                        break;
                    }
                }

                if (isAllEmpty)
                {
                    // 完成当前波次
                    StreetEventHittableTargetGroupComplete.OnGroupComplete?.Invoke(_groupId);
                }
            }
            
        }

        /// <summary>
        /// 下一波次
        /// </summary>
        public void NextGroup()
        {
            int nextGroupId;
            if (_totalGroupCount >= _totalGroupIdSettings.Count)
            {
                nextGroupId = _totalGroupIdSettings[_totalGroupIdSettings.Count - 1];
            }
            else
            {
                nextGroupId = _totalGroupIdSettings[_totalGroupCount];
            }
            if (_groupId!=nextGroupId && !_noticedGroupIds.Contains(nextGroupId))
            {
                // 显示提示
                _noticedGroupIds.Add(nextGroupId);
                ResidentHandler.Inst.GetFunction("CityLuaFunction.showStreetEventShootingStageNotice").Action(nextGroupId);
            }
            _groupId = nextGroupId;
            _totalGroupCount++;
        }
        
        /// <summary>
        /// 刷新所有轨道
        /// </summary>
        public void RefreshAllLines(bool forceRefresh = false)
        {
            if (!CanRefresh && !forceRefresh)
            {
                return;
            }
            
            foreach (var line in _lines)
            {
                line.RefreshLine();
            }
        }
        
        /// <summary>
        /// 获取轨道
        /// </summary>
        /// <param name="lineId"></param>
        /// <returns></returns>
        public StreetEventHittableTargetLine GetLine(int lineId)
        {
            return _linesMap[lineId];
        } 
        
    }
}